package fr.guy.aurelie.eportfolio.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;

import fr.guy.aurelie.eportfolio.client.event.WelcomeEvent;
import fr.guy.aurelie.eportfolio.client.event.WelcomeEventHandler;
import fr.guy.aurelie.eportfolio.client.presenter.MyCoursePresenter;
import fr.guy.aurelie.eportfolio.client.presenter.MyExperiencePresenter;
import fr.guy.aurelie.eportfolio.client.presenter.MyInfosPresenter;
import fr.guy.aurelie.eportfolio.client.presenter.Presenter;
import fr.guy.aurelie.eportfolio.client.presenter.WelcomePresenter;
import fr.guy.aurelie.eportfolio.client.view.MyCourseView;
import fr.guy.aurelie.eportfolio.client.view.MyExperienceView;
import fr.guy.aurelie.eportfolio.client.view.MyInfosView;
import fr.guy.aurelie.eportfolio.client.view.WelcomeView;

public class AppController implements Presenter, ValueChangeHandler<String> {
	private static final String MY_EXPERIENCE = "mes_experiences";
	private static final String MY_COURSE = "mon_cursus";
	private static final String WELCOME = "mon_projet";
	private static final String MY_INFOS = "mes_infos";
	private final HandlerManager eventBus;
	private final EPortfolioServiceAsync rpcService;
	private HasWidgets container;

	MenuBar menu = new MenuBar();
	Command welcomeCmd = new Command() {
		public void execute() {
			doWelcome();
		}
	};
	Command myInfosCmd = new Command() {
		public void execute() {
			doMyInfos();
		}
	};
	Command myCourseCmd = new Command() {
		public void execute() {
			doMyCourse();
		}
	};
	Command myExperienceCmd = new Command() {
		public void execute() {
			doMyExperience();
		}
	};

	MenuItem myInfosItem = new MenuItem("Mes infos", myInfosCmd);
	MenuItem welcomeItem = new MenuItem("Mon projet", welcomeCmd);
	MenuItem myCourseItem = new MenuItem("Mon cursus", myCourseCmd);
	MenuItem myExperienceItem = new MenuItem("Mes expériences", myExperienceCmd);

	public AppController(EPortfolioServiceAsync rpcService,
			HandlerManager eventBus) {
		this.rpcService = rpcService;
		this.eventBus = eventBus;
		bind();
	}

	private void bind() {
		History.addValueChangeHandler(this);

		doMenu();
		
		ClickHandler headerClick = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Window.alert("testa");
				doMyInfos();
			}
		};
		RootPanel.get("header").addHandler(headerClick, ClickEvent.getType());

		eventBus.addHandler(WelcomeEvent.TYPE, new WelcomeEventHandler() {
			@Override
			public void onWelcome(WelcomeEvent event) {
				doWelcome();
			}
		});
	}

	// FIXME
	private void doMenu() {
		menu.addItem(myInfosItem);
		menu.addItem(welcomeItem);
		menu.addItem(myCourseItem);
		menu.addItem(myExperienceItem);

		// menu.setAnimationEnabled(true);

		RootPanel.get("menu").add(menu);
	}

	protected void doMyExperience() {
		History.newItem(MY_EXPERIENCE);
	}

	protected void doMyCourse() {
		History.newItem(MY_COURSE);
	}

	private void doMyInfos() {
		History.newItem(MY_INFOS);
	}

	private void doWelcome() {
		History.newItem(WELCOME);
	}

	@Override
	public void go(final HasWidgets container) {
		this.container = container;

		if ("".equals(History.getToken())) {
			History.newItem(MY_INFOS);
		} else {
			History.fireCurrentHistoryState();
		}
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();
		Presenter presenter = null;

		if (token != null) {

			if (WELCOME.equals(token)) {
				presenter = new WelcomePresenter(rpcService, eventBus,
						new WelcomeView());
				welcomeItem.addStyleName("gwt-MenuItem-selected");
				myInfosItem.removeStyleName("gwt-MenuItem-selected");
				myCourseItem.removeStyleName("gwt-MenuItem-selected");
				myExperienceItem.removeStyleName("gwt-MenuItem-selected");
			} else if (MY_INFOS.equals(token)) {
				presenter = new MyInfosPresenter(rpcService, eventBus,
						new MyInfosView());
				myInfosItem.addStyleName("gwt-MenuItem-selected");
				welcomeItem.removeStyleName("gwt-MenuItem-selected");
				myCourseItem.removeStyleName("gwt-MenuItem-selected");
				myExperienceItem.removeStyleName("gwt-MenuItem-selected");
			} else if (MY_COURSE.equals(token)) {
				presenter = new MyCoursePresenter(rpcService, eventBus,
						new MyCourseView());
				myCourseItem.addStyleName("gwt-MenuItem-selected");
				myInfosItem.removeStyleName("gwt-MenuItem-selected");
				welcomeItem.removeStyleName("gwt-MenuItem-selected");
				myExperienceItem.removeStyleName("gwt-MenuItem-selected");
			} else if (MY_EXPERIENCE.equals(token)) {
				presenter = new MyExperiencePresenter(rpcService, eventBus,
						new MyExperienceView());
				myExperienceItem.addStyleName("gwt-MenuItem-selected");
				myInfosItem.removeStyleName("gwt-MenuItem-selected");
				myCourseItem.removeStyleName("gwt-MenuItem-selected");
				welcomeItem.removeStyleName("gwt-MenuItem-selected");
			}

		}
		// else {
		// presenter = new WelcomePresenter(rpcService, eventBus,
		// new WelcomeView());
		// }

		if (presenter != null) {
			presenter.go(container);
		}

	}
}
